<?php

/**
 * This is the model class for table "category".
 *
 * The followings are the available columns in table 'category':
 * @property integer $id
 * @property integer $parent_id
 * @property string $title
 * @property string $alt_title
 * @property string $description
 * @property string $keywords
 */
class Category extends CActiveRecord {

    public static $list = array();

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'category';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('parent_id, title', 'required'),
            array('parent_id', 'numerical', 'integerOnly' => true),
            array('title, alt_title', 'length', 'max' => 50),
            array('description, keywords', 'length', 'max' => 1000),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, parent_id, title, alt_title, description, keywords', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'parent' => array(self::BELONGS_TO, 'Category', 'parent_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'parent_id' => 'Родитель',
            'title' => 'Заголовок',
            'alt_title' => 'Ссылка',
            'description' => 'Description',
            'keywords' => 'Keywords',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('parent_id', $this->parent_id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('alt_title', $this->alt_title, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('keywords', $this->keywords, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Category the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static function getCategoryList() {
        $criteria = new CDbCriteria;
        $criteria->select = 'id,title';  // выбираем только поле 'id,title'
        $criteria->condition = 'parent_id=:parent_id';
        $criteria->params = array(':parent_id' => 0);

        $model = self::model()->findAll($criteria);

        self::$list[] = array('id' => 0, 'title' => 'Главная категория');

        foreach ($model as $item) {
            self::$list[] = array('id' => $item->id, 'title' => $item->title);
            self::getPodCategory($item->id);
        }
        /* if($param == 'post')
          {
          $model = self::model()->getPodCategory($model);
          }

          if($param == '') */

        return CHtml::listData(self::$list, 'id', 'title');
    }

    public function getPodCategory($id, $sep = '') {
        $sep.='-';
        $criteria = new CDbCriteria;
        $criteria->select = 'id,title';
        $podcat_list = self::model()->findAllByAttributes(
                array(
            'parent_id' => $id
                ), $criteria
        );

        foreach ($podcat_list as $item) {
            self::$list[] = array('id' => $item->id, 'title' => $sep . $item->title);
            self::getPodCategory($item->id, $sep);
        }

        return self::$list;
    }

    public static function getMenu() {
        $criteria = new CDbCriteria;
        $criteria->select = 'title,alt_title';  // выбираем только поле 'title'
        $criteria->condition = 'parent_id=:parent_id';

        $criteria->params = array(':parent_id' => 0);

        $model = self::model()->findAll($criteria);

        //$model = self::model()->getPodCategoryMenu($model);  

        return $model;
    }

    public function beforeSave() {
        //if($this->isNewRecord)
//            $this->alt_title = $this->title.'-link';
        $this->alt_title = Translite::rusencode($this->title);
        return parent::beforeSave();
    }

}
